// Fill out your copyright notice in the Description page of Project Settings.


#include "LobbyGameMode.h"
#include "GameFramework/GameStateBase.h"
#include "MultiplayerSessionsSubsystem.h"

void ALobbyGameMode::PostLogin(APlayerController* NewPlayer)
{
	Super::PostLogin(NewPlayer);
	//GameState.Get()：通过Get函数访问游戏状态基指针
	//PlayerArray：客户端和服务器玩家数量数组
	int32 NumberOfPlayers = GameState.Get()->PlayerArray.Num();
	UGameInstance* GameInstance = GetWorld()->GetGameInstance();
	if(GameInstance)
	{
		UMultiplayerSessionsSubsystem* Subsystem = GameInstance->GetSubsystem<UMultiplayerSessionsSubsystem>();
		check(Subsystem);
		if(NumberOfPlayers == Subsystem->DesiredNumPublicConnections)
		{
			UWorld* World = GetWorld();
			if(World)
			{
				//bUseSeamlessTravel：是否使用无缝过渡
				bUseSeamlessTravel = true;
				FString MatchType = Subsystem->DesiredMatchType;
				if(MatchType == "FreeForAll")
				{
					//ServerTravel：将整个服务切换到新的关卡或地址
					World->ServerTravel("/Game/Maps/BlasterMap?listen");
				}else if(MatchType == "Teams")
				{
					World->ServerTravel("/Game/Maps/Teams?listen");
				}else if(MatchType == "CaptureTheFlag")
				{
					World->ServerTravel("/Game/Maps/CaptureTheFlag?listen");
				}

			}
		}
	}
}

void ALobbyGameMode::Logout(AController* Existing)
{
	Super::Logout(Existing);
}
